<?php
	//handle post events.
	if(isset($_POST['login_submit'])) {
		$email = $_POST['email'];
		$password = makePassword($_POST['password']);
		$query = mysql_query("SELECT * FROM `instructor` WHERE `email`='$email' AND `password`='$password' AND `permission` = '2'") or die(mysql_error());
		$result = mysql_fetch_object($query);
		if($result) {
			$_SESSION['logged_in'] = $result->id;
			setcookie('etat_logged_in', $result->id, time()+COOKIE_TIME);
			unset($_SESSION['m']);
			redirect("home.php");
		} else {
			$_SESSION['m'] = 2;
			redirect("index.php");
		}
	}

	if(isset($_POST['forgot_pass_submit'])) {
		$email = $_POST['email'];
		$query = mysql_query("SELECT * FROM `instructor` WHERE `email`='$email'") or die(mysql_error());
		$result = mysql_fetch_object($query);
		if($result) {
			//@TODO: reset password and send email
			redirect('index.php?m=4');
		} else {
			redirect('index.php?m=6');
		}
	}

	if (isset($_POST['logout_submit'])) {
		logout();
	}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=encoding">
<title><?php print $page_title; ?></title>
<?php
	foreach($java_script as $js) {
		if(file_exists(ROOT . BRANCH . "/includes/js/$js.js"))
		print "<script language='JavaScript' type='text/javascript' src='/includes/js/$js.js'></script>";
	}
	foreach($style_sheet as $css) {
		if(file_exists(ROOT . BRANCH . "/includes/css/$css.css"))
		print "<link href='/includes/css/$css.css' rel='stylesheet' type='text/css'>";
	}
?>
</head>
<body>
	<div id="wrapper">
	<div id="fade" class='invisible' onclick='closePopup();'></div>
	<div id="page-top">&nbsp;</div>
	<div id="page-container">
		<?php if(!isLoggedIn()) { ?>
			<div id='index-header'>
			<?php
				print imageAnchor('/','etat-logo','etat-logo.jpg',110,70);
				print '<div id="login-form" class="right">';
				print '<div id="login-btn"><p>Welcome to eTAT: ';
				print newBtn('login','Login',array('type'=>'button','onclick'=>'popup("login-box")','class'=>'right'),false);
				print '</p></div>';
				$form1 = new Form('user_login_form','post',true);
				$form1->textInput('email','Gmail Address');
				$form1->textInput('password','Password',array('type'=>'password'));
				$form1->button('login_submit','Login',array('type'=>'submit','class'=>'right'),false);
				$form1->button('login_cancel','Cancel',array('type'=>'button','class'=>'right','onclick'=>'closePopup();'),false);
				$form1->button('forgot_pass','Forgot Username/Password',array('type'=>'button','class'=>'right','onclick'=>'javascript:forgotPass();'));
				$form1->text("<div class='clear'></div>");
				$form2 = new Form('forgot_pass_form','post',true,array('class'=>'invisible'));
				$form2->textInput('lost_email','Gmail Address');
				$form2->button('forgot_pass_submit','Reset Password',array('type'=>'submit'),false);
				$form2->button('cancel_pass','Cancel',array('type'=>'button','onclick'=>'javascript:cancelForgotPass();'),false);
				print popup('login-box',$form1->build().$form2->build(),250,array('top'=>10,'right'=>10));
				print '</div>';
			?>
		<?php
		}
		if(isset($_GET['m']) || isset($_SESSION['m'])) {
			if(isset($_GET['m'])) {
				$_SESSION['m'] = $_GET['m'];
			}
			$m = $_SESSION['m'];
			unset($_SESSION['m']);
			switch($m) {
				case '1': $msg = "Logged out successfully"; break;
				case '2': $msg = "Invalid Username/Password, Please try again"; break;
				case '3': $msg = "You need to be logged in to view that page"; break;
				case '4': $msg = "Password reset successful, please check your email"; break;
				case '5': $msg = "Sorry, you cannot access that file."; break;
				case '6': $msg = "Sorry, that Email Address was not found in our database. Please contact the Administrator"; break;
				case '7': $msg = "Sorry, you need to be invited first in order to register. Please contact the Administrator."; break;
			}
			printMsg($msg);
			unset($_SESSION['m']);
		}
		if(isLoggedIn()) {
		?>
		<div id='header'>
		<?php
			print newBtn('etat_logo','eTAT',array('onclick'=>"redirect('home.php')",'type'=>'button'),false);
			require_once(ROOT . BRANCH . "/library/topnav.php");
		} ?>
	</div>
	<div id='content'>